<?php
/**
 * User: 魔十七
 * Date: 2020/11/6
 * Time: 13:11
 */

namespace Admin\Controller;

use think\Controller;
class AnnouncementController extends Controller
{
    public function index(){
        $this->display();//渲染视图
    }

    //获取列表
    public function getList(){
        $Announcement = M('announcement')->field("*,FROM_UNIXTIME(add_date,'%Y年%m月%d日') as add_date")->order('id desc')->page($_GET['page'].',20')->select();
        $count = M('announcement')->order('id desc')->count();
        $data = ['code'=>'0','msg'=>'','count'=>$count,'data'=>$Announcement];
        echo json_encode($data);
    }

    //添加
    public function create(){
        $this->display();//渲染视图
    }

    public function edit($id){
        $data=M('announcement')->where("id=$id")->find();
        if($data){
            $this->assign('data',$data);
            $this->display();
        }else{
            $this->error('异常');
        }
    }

    //保存
    public function save(){
        $go_back['code']=1001;
        $post=$_POST;
        $id=isset($post['id']) ? $post['id'] :'' ;
        if($id){
            unset($post['id']);
            $res = M('announcement')->where("id=$id")->save($post);
        }else{
            $post['add_date']=time();
            $res=M('announcement')->add($post);
        }
        if(!$res){
            $go_back['code']=1002;
        }
        $this->ajaxReturn($go_back);
    }

    //删除
    public function delete(){
        $go_back['code']=1001;
        $post=$_POST;
        $id=isset($post['id']) ? $post['id'] :$this->ajaxReturn(['code'=>1002]);
        M('announcement')->delete($id);
        $this->ajaxReturn($go_back);
    }
}